package cms.api.solr.index.service;

import cms.api.common.cofiguration.SolrApiProperties;
import cms.api.framework.search.IndexService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Solr集群索引服务实现类
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/29 18:00
 */
@Slf4j
@Service("solrCollectionServiceImpl")
public class SolrCollectionServiceImpl extends BaseSolrServiceImpl implements IndexService {

    @Autowired
    private SolrApiProperties properties;

    @Override
    public boolean createIndex(String index) {
        try {
            if (isIndexExist(index)) {
                log.info("要创建的索引[{}]已存在", index);
                return true;
            }
            CollectionAdminRequest.Create request = CollectionAdminRequest.createCollection(index, properties.getConfig(), properties.getNumShards(), properties.getNumReplicas());
            CollectionAdminResponse collectionAdminResponse = request.process(solrClient);
            JSONObject responseJsonObject = this.formatNamedList(collectionAdminResponse.getResponse());
            log.info("创建[{}]索引库响应消息[{}]", index, responseJsonObject);
            int status = collectionAdminResponse.getStatus();
            if (status == NumberUtils.INTEGER_ZERO.intValue()) {
                return responseJsonObject.containsKey("success");
            }
            return false;
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

    @Override
    public boolean deleteIndex(String index) {
        try {
            if (!isIndexExist(index)) {
                log.info("要删除的索引[{}]不存在", index);
                return true;
            }
            CollectionAdminRequest.Delete request = CollectionAdminRequest.deleteCollection(index);
            CollectionAdminResponse collectionAdminResponse = request.process(solrClient);
            JSONObject responseJsonObject = this.formatNamedList(collectionAdminResponse.getResponse());
            log.info("删除[{}]索引库响应消息[{}]", index, responseJsonObject);
            int status = collectionAdminResponse.getStatus();
            return status == NumberUtils.INTEGER_ZERO.intValue() && responseJsonObject.containsKey("success");
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

    @Override
    public boolean isIndexExist(String index) {
        try {
            List<String> indexs = CollectionAdminRequest.listCollections(solrClient);
            return indexs.contains(index);
        } catch (SolrServerException e) {
            log.error("Solr服务器通信/解析异常, index=[{}]", index, e);
        } catch (IOException e) {
            log.error("IO异常, index=[{}]", index, e);
        }
        return false;
    }

}
